package com.kingyea.mobilepolice.dictionary.service.impl;

import com.google.common.collect.Lists;
import com.kingyea.mobilepolice.dictionary.bean.Dictionary;
import com.kingyea.mobilepolice.dictionary.bean.DictionaryCondition;
import com.kingyea.mobilepolice.dictionary.bean.DictionaryVo;
import com.kingyea.mobilepolice.dictionary.dao.DictionaryMapper;
import com.kingyea.mobilepolice.dictionary.service.DictionaryService;
import com.kingyea.mobilepolice.orgjobcategory.bean.OrgDictionaryJobCategory;
import com.kingyea.mobilepolice.orgjobcategory.bean.OrgDictionaryJobCategoryCondition;
import com.kingyea.mobilepolice.orgjobcategory.bean.OrgDictionaryJobCategoryPo;
import com.kingyea.mobilepolice.orgjobcategory.dao.OrgDictionaryJobCategoryMapper;
import com.kingyea.mobilepolice.system.StateCode;
import com.kingyea.mobilepolice.util.ExceptionConditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hx
 * @version v1.0.0
 * @description Dictionary 实现类
 * @date Created in 2018-04-12 16:42:54
 */
@Service
@Transactional
public class DictionaryServiceImpl implements DictionaryService {

    private static final Logger LOGGER = LoggerFactory.getLogger(DictionaryServiceImpl.class);

    @Resource
    private DictionaryMapper dictionaryMapper;

    @Resource
    private OrgDictionaryJobCategoryMapper orgDictionaryJobCategoryMapper;

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @Override
    public Dictionary getById(Integer id) throws Exception {
        return this.dictionaryMapper.getById(id);
    }

    /**
     * 列表
     *
     * @param
     * @return
     */
    @Override
    public List<Dictionary> search(DictionaryCondition condition) throws Exception {
        List<Dictionary> list = dictionaryMapper.search(condition);
        return list;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) throws Exception {
        dictionaryMapper.deleteById(id);
        return 1;
    }

    /**
     * 保存
     *
     * @param dictionary
     * @return
     */
    @Override
    public Dictionary save(Dictionary dictionary) throws Exception {
        try {
            dictionaryMapper.save(dictionary);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dictionary;
    }

    /**
     * 更新
     *
     * @param dictionary
     * @return
     */
    @Override
    public Dictionary update(Dictionary dictionary) throws Exception {
        try {
            dictionaryMapper.update(dictionary);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dictionary;
    }

    @Override
    public List<DictionaryVo> searchTree(DictionaryCondition condition) {
        List<DictionaryVo> topDictionaryList = Lists.newArrayList();
        condition.setPageAble(false);

        List<Dictionary> list = dictionaryMapper.search(condition);

        List<DictionaryVo> voList = list.stream().map(bean -> {
            DictionaryVo vo = new DictionaryVo();
            BeanUtils.copyProperties(bean, vo);
            if (bean.getPid() == -1) {
                topDictionaryList.add(vo);
            }
            return vo;
        }).collect(Collectors.toList());

        treeOp(voList);
        return topDictionaryList;
    }

    private void treeOp(List<DictionaryVo> voList) {
        voList.stream().forEach(vo1 -> {
            List<DictionaryVo> childrenList = voList.stream().filter(vo2 -> vo1.getId().equals(vo2.getPid())).collect(Collectors.toList());
            vo1.setDictionaryVos(childrenList);
        });
    }


    @Override
    @Transactional
    public List<Dictionary> searchByOrgSerialNo(OrgDictionaryJobCategoryCondition condition) {
        condition.setPageAble(false);
        List<OrgDictionaryJobCategory> orgDictionaryJobCategoryList = orgDictionaryJobCategoryMapper.search(condition);
        if (orgDictionaryJobCategoryList.size() != 0) {
            DictionaryCondition dictionaryCondition = new DictionaryCondition();
            dictionaryCondition.setPageSize(condition.getPageSize());
            dictionaryCondition.setPageNum(condition.getPageNum());
            dictionaryCondition.setType("jobCategory");
            dictionaryCondition.setDictionaryIds(orgDictionaryJobCategoryList.stream().map(OrgDictionaryJobCategory::getDictionaryId).distinct().collect(Collectors.toList()));
            List<Dictionary> list = dictionaryMapper.search(dictionaryCondition);
            condition.setTotal(dictionaryCondition.getTotal());
            return list;
        }
        return Lists.newArrayList();
    }


    @Override
    @Transactional
    public Boolean updateForOrg(OrgDictionaryJobCategoryPo po) {
        ExceptionConditions.checkNotNull(po, StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkNotNull(po.getDistionaryIds(), StateCode.NO_ARGUMENT_ERROR);
        ExceptionConditions.checkStringNotEmpty(po.getOrgSerialNo(), StateCode.NO_ARGUMENT_ERROR);
        // 不处理了
        orgDictionaryJobCategoryMapper.deleteByOrgSerialNo(po.getOrgSerialNo());
        if (po.getDistionaryIds().size() > 0) {
            return orgDictionaryJobCategoryMapper.batchSave(po.getDistionaryIds().stream().map(distionaryId -> {
                OrgDictionaryJobCategory orgDictionaryJobCategory = new OrgDictionaryJobCategory();
                orgDictionaryJobCategory.setOrgSerialNo(po.getOrgSerialNo());
                orgDictionaryJobCategory.setDictionaryId(distionaryId);
                return orgDictionaryJobCategory;
            }).collect(Collectors.toList())) == po.getDistionaryIds().size();
        }
        return Boolean.TRUE;
    }

}
